<?php


namespace app\controller\admin;


use app\models\shop\ShopModel;
use app\models\user\UserModel;
use think\facade\Db;
use think\Request;

class ShopController extends BaseAdminController
{
    function setSearch()
    {
        $where = [];
        $invite = $this->request->param('invite', '', 'trim');
        if ($invite) {
            $uidArr = UserModel::where('mobile|nick', 'like', '%' . $invite . '%')->value('id');
            $where[] = ['invite_uid', 'in', $uidArr];
        }
        return [
            'like' => ['shop_name', 'username', 'mobile'],
            '=' => ['status'],
            'custom' => $where
        ];
    }

    function setSort()
    {
        return [
            'inviter_name' => 'inviter_uid',
            'status_attr' => 'status',
        ];
    }

    public function getList()
    {
        try {
            $where = $this->searchWhere;
            $orderBy = $this->orderBy;
            $sort = $this->sort;
            $list = ShopModel::where($where)
                ->order($orderBy, $sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['status_attr'] = $v['status'] == 0 ? '待审核' : ($v['status'] == 1 ? '审核通过' : '审核拒绝');
                $userInfo = UserModel::find($v['invite_uid']);
                $v['inviter_name'] = $userInfo['nick'] ?? "";
                $v['inviter_mobile'] = $userInfo['mobile'] ?? '';
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        try {
            $data = $this->request->param('', '', 'trim');
            $model = ShopModel::find($data['id']);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            $data['invite_uid'] = UserModel::where('mobile', $data['invite_mobile'])->value('id', 0);
            $model->save($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 审核
     * @return \think\response\Json
     */
    public function status()
    {
        try {
            $data = $this->request->param('', '', 'trim');
            $model = ShopModel::find($data['id']);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            if ($model->status != 0) {
                throw new \Exception("店铺状态错误");
            }
            if ($model->status == $data['status']) {
                throw new \Exception("请选择审核状态");
            }
            unset($data['create_time']);
            $data['update_time'] = time();

            $model->save($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        try {
            $id = $this->request->param('id');
            $model = ShopModel::find($id);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            $model->is_del = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 店铺列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function getShopList(Request $request)
    {
        try {
            $userInfo = $request->userInfo;
            $where = [];
            $where[] = ['is_del', '=', 0];
            $where[] = ['status', '=', 1];
            $list = ShopModel::where($where)
                ->order('id', 'desc')
                ->select()
                ->toArray();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}